package com.banciyuan.bcywebview.biz.main.mineinfo.MySeries;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.biz.detail.noveldetail.NovelIndexActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.SeriesItem;

/**
 * Created by hy on 2015/9/15.
 */
public class WorkSeriesAdapter extends BaseAdapter {

    private List<SeriesItem> seriesItems;
    private Context context;
    private ImageLoader imageLoader;

    public WorkSeriesAdapter(Context context, List<SeriesItem> seriesItems) {
        this.context = context;
        this.seriesItems = seriesItems;
        imageLoader = ImageLoader.getInstance();
    }

    @Override
    public int getCount() {
        return seriesItems.size();
    }

    @Override
    public Object getItem(int i) {
        return seriesItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        SeriesItem seriesItem = seriesItems.get(position);
        ViewHolder holder;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.workseriesitem, null);
            holder.seriesTitle = (TextView) convertView.findViewById(R.id.tv_workseries);
            holder.seriesRead = (TextView) convertView.findViewById(R.id.tv_workread);
            holder.seriesChapter = (TextView) convertView.findViewById(R.id.tv_workchapter);
            holder.seriesImg = (ImageView) convertView.findViewById(R.id.iv_workseries);
            holder.seriesAuthor = (TextView) convertView.findViewById(R.id.tv_workauthor);
            holder.orderNum = (TextView) convertView.findViewById(R.id.tv_ordernum);
            holder.dingNum = (TextView) convertView.findViewById(R.id.tv_dingnum);
            holder.wordNum = (TextView) convertView.findViewById(R.id.tv_wordnum);
            holder.seriesSet = (ImageView) convertView.findViewById(R.id.iv_workset);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        imageLoader.displayImage(seriesItem.getCover(), holder.seriesImg, ImageOptions.options);
        if (!TextUtils.isEmpty(seriesItem.getTitle())) {
            holder.seriesTitle.setText(Html.fromHtml(seriesItem.getTitle()));
        }
        if (!TextUtils.isEmpty(seriesItem.getUname())) {
            holder.seriesAuthor.setText(context.getString(R.string.writer) + seriesItem.getUname());
        }
        String time = StringUtil.getOppositeTime(seriesItem.getMtime());
        if ("true".equals(seriesItem.getSet_end())) {
            holder.seriesSet.setImageResource(R.drawable.workset_written);
        } else {
            holder.seriesSet.setImageResource(R.drawable.workset_writting);
        }
        if (TextUtils.isEmpty(time)) {
            time = context.getString(R.string.recently);
        }
        if (null != seriesItem.getPost()) {
            holder.seriesChapter.setVisibility(View.VISIBLE);
            holder.seriesChapter.setText(Html.fromHtml(time + context.getString(R.string.update) + String.format(context.getString(R.string.work_series_title), seriesItem.getPost().getTitle())));
        } else {
            holder.seriesChapter.setVisibility(View.VISIBLE);
            holder.seriesChapter.setText(context.getString(R.string.no_more_update));
        }
        if (!TextUtils.isEmpty(seriesItem.getSubscribe_num())) {
            holder.orderNum.setText(seriesItem.getSubscribe_num());
        }
        if (!TextUtils.isEmpty(seriesItem.getDing_num())) {
            holder.dingNum.setText(seriesItem.getDing_num());
        }
        if (!TextUtils.isEmpty(seriesItem.getSet_word_count())) {
            holder.wordNum.setText(seriesItem.getSet_word_count());
        }
        String index = SPHelper.getString(context, SPConstant.SPNAME_BOOKMARK, seriesItem.getWns_id(), "0#0");
        String[] arrays = index.split("#");
        String rpid = "";
        if (!arrays[0].equals("0")) {
            rpid = arrays[0];
            holder.seriesRead.setText(context.getString(R.string.continues_read) + arrays[1] + context.getString(R.string.series_chapter));
        } else {
            holder.seriesRead.setText(context.getString(R.string.start_read));
            if (null != seriesItem.getSet_posts() && seriesItem.getSet_posts().size() > 0) {
                rpid = seriesItem.getSet_posts().get(0);
            }
        }
        OnClick onClick = new OnClick(position);
        if (!TextUtils.isEmpty(rpid)) {
            onClick.setRpid(rpid);
            holder.seriesRead.setOnClickListener(onClick);
        }
        holder.seriesChapter.setOnClickListener(onClick);
        return convertView;
    }

    class OnClick implements View.OnClickListener {

        int position;
        SeriesItem mItem;
        String rpid;

        public OnClick(int position) {
            this.position = position;
            this.mItem = (SeriesItem) getItem(position);
        }

        public void setRpid(String rpid) {
            this.rpid = rpid;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_workread:
                    DetailDataHelper.gotoWriterDetail(context, rpid, mItem.getWp_id(), mItem.getWns_id(), true);
                    break;
                case R.id.tv_workchapter:
                    if (null != mItem.getPost()) {
                        DetailDataHelper.gotoWriterDetail(context, mItem.getPost().getRp_id(), mItem.getWp_id(), mItem.getWns_id(), true);
                    } else {
                        gotoUtil.gotoAct(context, NovelIndexActivity.class, mItem.getWns_id());
                    }
                    break;
            }
        }
    }


    public void setdata(List<SeriesItem> seriesItems) {
        this.seriesItems = seriesItems;
    }

    class ViewHolder {
        TextView seriesTitle;
        TextView seriesChapter;
        TextView seriesRead;
        TextView seriesAuthor;
        ImageView seriesImg;
        TextView orderNum;
        TextView dingNum;
        TextView wordNum;
        ImageView seriesSet;
    }
}
